package myproxy

import (
	"ginplay/mytool"

	"encoding/json"
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
)

type GETPAGEREQUEST struct {
	Pagenow  int    `json:"pagenow"`
	Pagesize int    `json:"pagesize"`
	Search   string `json:"search"`
}

type PROXYRESULTS struct {
	Id      int    `json:"id"`
	Name    string `json:"name"`
	Address string `json:"address"`
	User    string `json:"user"`
	Key     string `json:"key"`
}

func getContent(onegetrequest GETPAGEREQUEST) []PROXYRESULTS {
	tsql := "select id,name,address,user,key from myproxy where name like ? limit ?,?"
	rows := mytool.SelectByPara(tsql, "%"+onegetrequest.Search+"%", (onegetrequest.Pagenow-1)*onegetrequest.Pagesize, onegetrequest.Pagesize)
	defer rows.Close()
	var returnresult []PROXYRESULTS
	for rows.Next() {
		var oneplay PROXYRESULTS
		rows.Scan(&oneplay.Id, &oneplay.Name, &oneplay.Address, &oneplay.User, &oneplay.Key)
		returnresult = append(returnresult, oneplay)
	}
	return returnresult
}

func getAllCount(onegetrequest GETPAGEREQUEST) string {
	tsql := "select count(1) as allcount from myproxy where name like ?"
	rows := mytool.SelectByPara(tsql, "%"+onegetrequest.Search+"%")
	defer rows.Close()
	var allcount string
	for rows.Next() {
		rows.Scan(&allcount)
	}
	return allcount
}

type PROXYRESULT struct {
	Result   []PROXYRESULTS `json:"result"`
	Allcount string         `json:"allcount"`
}

func GetByPage(c *gin.Context) {
	var onegetrequest GETPAGEREQUEST
	if err := c.BindJSON(&onegetrequest); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	fmt.Println(onegetrequest)
	myplaybook := getContent(onegetrequest)
	allcount := getAllCount(onegetrequest)
	var returnresult PROXYRESULT
	returnresult.Allcount = allcount
	returnresult.Result = myplaybook
	jsonData, _ := json.Marshal(returnresult)
	fmt.Println(string(jsonData))
	c.String(http.StatusOK, string(jsonData))
}

func Realadd(c *gin.Context) {
	var oneplay PROXYRESULTS
	if err := c.BindJSON(&oneplay); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	tsql := "insert into myproxy (name,address,user,key) VALUES( ?,?,?,? )"
	mytool.UpdateByPara(tsql, oneplay.Name, oneplay.Address, oneplay.User, oneplay.Key)
	c.String(http.StatusOK, string("添加成功"))
}

func GetContentByID(id string) PROXYRESULTS {
	tsql := "select id,name,address,user,key from myproxy where id = ?"
	rows := mytool.SelectByPara(tsql, id)
	defer rows.Close()
	var oneplay PROXYRESULTS
	for rows.Next() {
		rows.Scan(&oneplay.Id, &oneplay.Name, &oneplay.Address, &oneplay.User, &oneplay.Key)
	}
	return oneplay
}

func GetById(c *gin.Context) {
	id := c.Query("id")
	oneplay := GetContentByID(id)
	jsonData, _ := json.Marshal(oneplay)
	c.String(http.StatusOK, string(jsonData))
}

func Update(c *gin.Context) {
	var oneplay PROXYRESULTS
	c.BindJSON(&oneplay)
	tsql := "replace into myproxy (id, name,address,user,key) VALUES(?,?,?,?,?)"
	mytool.UpdateByPara(tsql, oneplay.Id, oneplay.Name, oneplay.Address, oneplay.User, oneplay.Key)
	c.String(http.StatusOK, string("更新成功"))
}

func DeleteById(c *gin.Context) {
	id := c.Query("id")
	tsql := "delete from myproxy where id = ?"
	mytool.UpdateByPara(tsql, id)
	c.String(http.StatusOK, string("删除成功"))
}

func Getnames(c *gin.Context) {
	tsql := "select name from myproxy"
	rows := mytool.SelectNoPara(tsql)
	defer rows.Close()
	var myNames []string
	for rows.Next() {
		var onename string
		rows.Scan(&onename)
		myNames = append(myNames, onename)
	}
	jsonData, _ := json.Marshal(myNames)
	c.String(http.StatusOK, string(jsonData))
}

func GetByName(name string) PROXYRESULTS {
	tsql := "select id,name,address,user,key from myproxy where name = ?"
	rows := mytool.SelectByPara(tsql, name)
	defer rows.Close()
	var oneresult PROXYRESULTS
	for rows.Next() {
		rows.Scan(&oneresult.Id, &oneresult.Name, &oneresult.Address, &oneresult.User, &oneresult.Key)
	}
	return oneresult
}

func CheckTable() {
	tsql := `select name from sqlite_master where name="myproxy"`
	rows := mytool.SelectNoPara(tsql)
	defer rows.Close()
	if rows.Next() {
		fmt.Println("myproxy is exits")
	} else {
		fmt.Println("create myproxy")
		csql := `
CREATE TABLE myproxy (
  id INTEGER PRIMARY KEY,
  name varchar(200) unique,
  address varchar(50) default '',
  user varchar(50) default '',
  key varchar(80) default '/root/.ssh/id_rsa'
);
        `
		mytool.UpdateNoPara(csql)
	}
}
